/**
 * 
 */
package com.tdkj.action.front.mall;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanType;
import com.tdkj.model.shop.BeanGood;
import com.tdkj.model.shop.BeanMallNotice;
import com.tdkj.model.shop.BeanOrderItem;
import com.tdkj.model.shop.BeanShop;
import com.tdkj.service.common.IBeanTypeService;
import com.tdkj.service.home.IBeanHomePageService;
import com.tdkj.service.mall.IMallService;
import com.tdkj.util.Page;

public class MallAction extends BaseAction {
	
	private IMallService mallService;
	
	private IBeanTypeService typeService;
	
	private IBeanHomePageService homePageService;
	
	
	public String execute(){
		
		String hqlShop = " from " + BeanShop.class.getName() + " as bean  order by createDate desc";
		Query queryShop = this.createQuery(hqlShop);
		
		Page shops = this.getBaseService().getBaseDao().findBy(queryShop, 1, 20);
		this.getReturnInfo().put("shops", shops);
		
		String hqlGoods = " from " + BeanGood.class.getName() + " as bean where bean.showcase!=null order by publishTime desc";
		Query queryGoods = this.createQuery(hqlGoods);
		
		Page goods = this.getBaseService().getBaseDao().findBy(queryGoods, 1, 20);
		this.getReturnInfo().put("goods", goods);
		
		String hqlShowcaseGoods = " from " + BeanGood.class.getName() + " as bean where bean.showcase!=null order by publishTime desc";
		Query queryShowcaseGoods = this.createQuery(hqlShowcaseGoods);
		
		Page showcaseGoods = this.getBaseService().getBaseDao().findBy(queryShowcaseGoods, 1, 20);
		this.getReturnInfo().put("showcaseGoods", showcaseGoods);
		
		String hqlAuction = " select bean from " + BeanGood.class.getName() + " as bean where bean.sellType='1' order by publishTime desc";
		Query queryAuction = this.createQuery(hqlAuction);
		
		Page auctionGoods = this.getBaseService().getBaseDao().findBy(queryAuction, 1, 20);
		this.getReturnInfo().put("auctionGoods", auctionGoods);
		
		
		String hqlNotice = " from " + BeanMallNotice.class.getName() + " as bean order by publishTime desc";
		Query queryNotice = this.createQuery(hqlNotice);
		
		Page mallNotices = this.getBaseService().getBaseDao().findBy(queryNotice, 1, 20);
		this.getReturnInfo().put("mallNotices", mallNotices);
		
		
		String hqlOrderItem = " from " + BeanOrderItem.class.getName() + " as bean order by bean.order.createTime desc";
		Query queryOrderItem = this.createQuery(hqlOrderItem);
		
		Page orderItems = this.getBaseService().getBaseDao().findBy(queryOrderItem, 1, 20);
		this.getReturnInfo().put("orderItems", orderItems);
		
		
		String hqlTypes = " from " + BeanType.class.getName() + " as bean where bean.rootName='GOOD_ROOT' and level='1'";
		Query queryTypes = this.createQuery(hqlTypes);
		
		this.getReturnInfo().put("types", queryTypes.list());
		
		this.getReturnInfo().put("requestInfos", homePageService.findTrades());
		
		return "success";
	}

	
	public String shopList(){
		
		String hqlTypes = " from " + BeanType.class.getName() + " as bean where bean.rootName='GOOD_ROOT' and level='1'";
		Query queryTypes = this.createQuery(hqlTypes);
		
		this.getReturnInfo().put("types", queryTypes.list());
		
		String hqlReShop = " from " + BeanShop.class.getName() + " as bean where bean.shopRecommendationUseInfo is not null and bean.shopRecommendationUseInfo.state='1' order by createDate desc";
		Query queryReShop = this.createQuery(hqlReShop);
		
		Page reShops = this.getBaseService().getBaseDao().findBy(queryReShop, 1, 20);
		this.getReturnInfo().put("reShops", reShops);
		
		String hqlShop = " from " + BeanShop.class.getName() + " as bean  order by createDate desc";
		Query queryShop = this.createQuery(hqlShop);
		
		Page shops = this.getBaseService().getBaseDao().findBy(queryShop, 1, 20);
		this.getReturnInfo().put("shops", shops);
		
		this.getReturnInfo().put("requestInfos", homePageService.findTrades());
		
		String hqlGoods = " from " + BeanGood.class.getName() + " as bean where bean.showcase!=null order by publishTime desc";
		Query queryGoods = this.createQuery(hqlGoods);
		
		Page goods = this.getBaseService().getBaseDao().findBy(queryGoods, 1, 20);
		this.getReturnInfo().put("goods", goods);
		return "shopList";
	}
	
	
	public String goodList(){
		
		List<BeanType> typeLevels = new ArrayList<BeanType>();
		
		String id = this.getQueryInfo().get("id");
		if(id!=null && !"".equals(id)){
			BeanType type = mallService.findById(Long.parseLong(id), BeanType.class);
			this.getReturnInfo().put("type", type);
			while(type.getParent()!=null){
				typeLevels.add(0,type);
				type = type.getParent();
			}
			this.getReturnInfo().put("typeLevels", typeLevels);
		}else{
			this.getReturnInfo().put("type", typeService.getRoot(IBeanTypeService.Constants.ROOT_NAME_OF_GOOD));
		}
		
		String fullId = this.getQueryInfo().get("fullId");
		String hql = " from " + BeanGood.class.getName() + " as bean where 1=1 " ;
		if(fullId!=null && !"".equals(fullId)){
			hql = hql + " and (bean.type.fullId='"+fullId+"' or bean.type.fullId like '"+fullId+"-%') ";
		}
		
		String keywords = this.getQueryInfo().get("keywords");
		if(keywords!=null && !"".equals(keywords)){
			hql = hql + " and (bean.name like '%"+keywords+"%' or bean.keywords like '%"+keywords+"%' or bean.detailDescription like '%"+keywords+"%') ";
		}
		
		hql = hql + " order by bean.publishTime desc";
		Query query = this.createQuery(hql);
		
		this.queryByPage(query, "mallMain!goodList.action", 10);
		
		String hqlTypes = " from " + BeanType.class.getName() + " as bean where bean.rootName='GOOD_ROOT' and level='1'";
		Query queryTypes = this.createQuery(hqlTypes);
		
		this.getReturnInfo().put("types", queryTypes.list());
		
		this.getReturnInfo().put("requestInfos", homePageService.findTrades());
		
		String hqlGoods = " from " + BeanGood.class.getName() + " as bean where bean.showcase!=null order by publishTime desc";
		Query queryGoods = this.createQuery(hqlGoods);
		
		Page goods = this.getBaseService().getBaseDao().findBy(queryGoods, 1, 20);
		this.getReturnInfo().put("goods", goods);
		
		return "goodList";
	}
	

	public String auctionList(){
		
		List<BeanType> typeLevels = new ArrayList<BeanType>();
		
		String id = this.getQueryInfo().get("id");
		if(id!=null && !"".equals(id)){
			BeanType type = mallService.findById(Long.parseLong(id), BeanType.class);
			this.getReturnInfo().put("type", type);
			while(type.getParent()!=null){
				typeLevels.add(0,type);
				type = type.getParent();
			}
			this.getReturnInfo().put("typeLevels", typeLevels);
		}else{
			this.getReturnInfo().put("type", typeService.getRoot(IBeanTypeService.Constants.ROOT_NAME_OF_GOOD));
		}
		
		String fullId = this.getQueryInfo().get("fullId");
		String hql = " from " + BeanGood.class.getName() + " as bean where 1=1 and bean.sellType='1' " ;
		if(fullId!=null && !"".equals(fullId)){
			hql = hql + " and (bean.type.fullId='"+fullId+"' or bean.type.fullId like '"+fullId+"-%') ";
		}
		hql = hql + " order by bean.publishTime desc";
		Query query = this.createQuery(hql);
		
		this.queryByPage(query, "mallMain!auctionList.action", 10);
		
		String hqlTypes = " from " + BeanType.class.getName() + " as bean where bean.rootName='GOOD_ROOT' and level='1'";
		Query queryTypes = this.createQuery(hqlTypes);
		
		this.getReturnInfo().put("types", queryTypes.list());
		
		this.getReturnInfo().put("requestInfos", homePageService.findTrades());
		
		String hqlGoods = " from " + BeanGood.class.getName() + " as bean where bean.showcase!=null order by publishTime desc";
		Query queryGoods = this.createQuery(hqlGoods);
		
		Page goods = this.getBaseService().getBaseDao().findBy(queryGoods, 1, 20);
		this.getReturnInfo().put("goods", goods);
		
		return "auctionList";
	}
	
	
	public String custom(){
		String hqlTypes = " from " + BeanType.class.getName() + " as bean where bean.rootName='GOOD_ROOT' and level='1'";
		Query queryTypes = this.createQuery(hqlTypes);
		
		this.getReturnInfo().put("types", queryTypes.list());
		
		return "custom";
	}
	
	public String creditGuarantee(){
		String hqlTypes = " from " + BeanType.class.getName() + " as bean where bean.rootName='GOOD_ROOT' and level='1'";
		Query queryTypes = this.createQuery(hqlTypes);
		
		this.getReturnInfo().put("types", queryTypes.list());
		
		return "creditGuarantee";
	}
	
	public String notice(){
		String noticeIdString= this.getQueryInfo().get("noticeId");
		Long noticeId = Long.parseLong(noticeIdString);
		
		BeanMallNotice notice = this.getBaseService().findById(noticeId, BeanMallNotice.class);
		this.getReturnInfo().put("notice", notice);
		
		String hqlTypes = " from " + BeanType.class.getName() + " as bean where bean.rootName='GOOD_ROOT' and level='1'";
		Query queryTypes = this.createQuery(hqlTypes);
		
		this.getReturnInfo().put("types", queryTypes.list());
		
		return "notice";
	}

	@Resource
	public void setMallService(IMallService mallService) {
		this.mallService = mallService;
	}

	@Resource
	public void setTypeService(IBeanTypeService typeService) {
		this.typeService = typeService;
	}

	@Resource
	public void setHomePageService(IBeanHomePageService homePageService) {
		this.homePageService = homePageService;
	}

}
